package com.smiier.skin;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.qrcode.QRCodeView;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.DoctorReserveDetailExtra;
import com.smiier.skin.extra.TreatementProjectExtra;
import com.smiier.skin.net.DoctorReserveDetailTask;
import com.smiier.skin.net.DoctorReserveDetailTask.DoctorReserveDetail;
import com.smiier.skin.net.DoctorReserveDetailTask.DoctorReserveDetailRequest;
import com.smiier.skin.net.DoctorReserveDetailTask.DoctorReserveDetailResponse;
import com.smiier.skin.net.DoctorReserveListTask.DoctorReserveItem;
import com.smiier.skin.net.UserGetTask;
import com.smiier.skin.net.UserGetTask.UserGetRequest;
import com.smiier.skin.net.UserGetTask.UserGetResponse;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class MyTreatmentProjectActivity extends BasicActivity implements
		OnClickListener {
	OImageView mImg, mDoctorImg;
	TextView mTreatmentName;
	TextView mHospitalName;
	TextView mPrice;
	TextView mStatus;
	TextView mNo;
	TextView mCreateTime;
	TextView mDocTime;
	TextView mReserveStatus;
	TreatementProjectExtra extra = new TreatementProjectExtra();
	LinearLayout mShowDoctor;
	LinearLayout mShowErWeima;
	TextView mCreateComment;
	TextView mOnlineSession;
	DoctorReserveDetail mDetail;
	QRCodeView mCode;
	TextView mDoctorName, mDoctorTitle, mDoctorHospital;
	User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_treatment_detail);
		init();
		Intent i = getIntent();
		extra.getFrom(i);
		DoctorReserveItem item = extra.getUser();
		setNavTitle("我的项目");
		mImg = findViewById(R.id.img_treatment, OImageView.class);
		LayoutParams params = (LayoutParams) mImg.getLayoutParams();
		params.leftMargin = 0;
		mImg.setLayoutParams(params);
		mTreatmentName = findViewById(R.id.treatment_name, TextView.class);
		mHospitalName = findViewById(R.id.treatment_hospital, TextView.class);
		mPrice = findViewById(R.id.treatment_price, TextView.class);
		mStatus = findViewById(R.id.treatment_status, TextView.class);
		mNo = findViewById(R.id.reserve_num, TextView.class);
		mCreateTime = findViewById(R.id.reserve_time, TextView.class);
		mDocTime = findViewById(R.id.reserve_doctor_time, TextView.class);
		mReserveStatus = findViewById(R.id.reserve_status, TextView.class);
		mCreateComment = findViewById(R.id.create_comment, TextView.class);
		mOnlineSession = findViewById(R.id.online_session, TextView.class);
		mCreateComment.setOnClickListener(this);
		mOnlineSession.setOnClickListener(this);
		mStatus.setVisibility(View.GONE);
		mShowDoctor = findViewById(R.id.show_doctor, LinearLayout.class);
		mShowErWeima = findViewById(R.id.show_erweima, LinearLayout.class);
		DoctorReserveDetailTask task = new DoctorReserveDetailTask();
		DoctorReserveDetailRequest req = new DoctorReserveDetailRequest();
		mCode = findViewById(R.id.erweima, QRCodeView.class);
		mDoctorImg = findViewById(R.id.imgLayout_show, OImageView.class);
		mDoctorName = findViewById(R.id.text_doctor_name, TextView.class);
		mDoctorTitle = findViewById(R.id.text_doctor_title, TextView.class);
		mDoctorHospital = findViewById(R.id.text_doctor_hospital,
				TextView.class);
		req.reserveid = item.id;
		task.addListener(new NetTaskListener<DoctorReserveDetailTask.DoctorReserveDetailRequest, DoctorReserveDetailTask.DoctorReserveDetailResponse>() {

			@Override
			public void onException(
					INetTask<DoctorReserveDetailRequest, DoctorReserveDetailResponse> task,
					Exception e) {

			}

			@Override
			public void onComplete(
					INetTask<DoctorReserveDetailRequest, DoctorReserveDetailResponse> task,
					DoctorReserveDetailResponse response) {
				if (response != null && response.size() > 0) {
					mDetail = response.get(0);
					mBitmapUtils.display(mImg, mDetail.url);
					mTreatmentName.setText(mDetail.name);
					mHospitalName.setText(mDetail.hospital);
					mPrice.setText(mDetail.discount_price + "");
					mReserveStatus.setText(mDetail.status);
					mNo.setText(mDetail.reserve_no);
					mCreateTime.setText(mDetail.ctime.toString());
					StringBuilder sb = new StringBuilder();
					sb.append(mDetail.booktime.toShotString()).append(
							" " + mDetail.is_afternoon == null ? "上午" : "下午");
					// String str = item.booktime.toString().substring(0, 10)
					// + item.is_afternoon == null ? "上午" : "下午";
					mDocTime.setText(sb.toString());
					if (mDetail.url != null && !response.equals("")) {
						mCode.setText(mDetail.url);
					}
					if (mDetail.status.equals("已消费")) {
						mShowDoctor.setVisibility(View.VISIBLE);
						loadDoctor();

					} else if (mDetail.status.equals("已确认")) {
						mShowErWeima.setVisibility(View.VISIBLE);
					}

				}
			}
		});
		task.setRequest(req);
		add(task);

	}

	private void loadDoctor() {
		UserGetTask task = new UserGetTask();
		UserGetRequest req = new UserGetRequest();
		req.uids = new ArrayList<Long>();
		req.uids.add(Long.parseLong(mDetail.doctor_id
		// 134
				+ ""));
		task.setRequest(req);
		task.addListener(new NetTaskListener<UserGetTask.UserGetRequest, UserGetTask.UserGetResponse>() {

			@Override
			public void onComplete(
					INetTask<UserGetRequest, UserGetResponse> task,
					UserGetResponse response) {
				if (response != null && response.ResultCode == 200) {
					if (response.Res.size() > 0) {
						user = response.Res.get(0);
						// mBitmapUtils.display(mDoctorImg, user.Pic,
						// new OBitmapLoadCallBack<View>());
						CommonUtility.displayHeadImage(mDoctorImg,
								mBitmapUtils, user.Pic, getResources(),
								user.Sex);
						mDoctorName.setText(user.Name);
						mDoctorHospital.setText(user.Hospital);
						StringBuilder sb = new StringBuilder();
						for (String str : user.Professional) {
							sb.append(str).append(",");
						}
						if (sb.toString().length() > 2) {
							mDoctorTitle.setText(sb.toString().substring(0,
									sb.toString().length() - 1));
						}
					}
				} else {

				}
			}

			@Override
			public void onException(
					INetTask<UserGetRequest, UserGetResponse> task, Exception e) {

			}
		});
		add(task);
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.create_comment) {
			Intent i = new Intent(this, CreateProjectCommentActivity.class);
			DoctorReserveDetailExtra extra = new DoctorReserveDetailExtra();
			extra.setUser(mDetail);
			if (extra.putTo(i)) {
				startActivity(i);
			}

		} else if (id == R.id.online_session) {
			Intent intent = new Intent(activity, ChatActivity.class);
			try {
				intent.putExtra(Constant.IDENTITY_KEY, JsonUtil.convert(user));
				intent.putExtra(Constant.KEY_TREATMENT, true);
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}
	
	
	/*@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
